<!-- 非油订单 -->
<template>
  <div class="app-container">
    <!-- 上部分表单 -->
    <el-form
      class="globalSerchFormStyle"
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
      label-position="right"
    >
      <el-row style="margin-top: 15px">
        <el-col
          :span="6"
          v-if="['', null].includes($store.state.user.userLevel)"
        >
          <el-form-item label="油企" prop="enterpriseId" size="small">
            <el-select
              style="margin-left: 15px"
              v-model="queryParams.enterpriseId"
              :clearable="true"
              filterable
              placeholder="请选择油企"
              @change="handleSelectChange"
            >
              <el-option
                v-for="(item, index) in enterpriseList"
                :key="index"
                :value="item.enterpriseId"
                :label="item.enterpriseName"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="6" v-if="!['2'].includes($store.state.user.userLevel)">
          <el-form-item label="油站" prop="stationId" clearable size="small">
            <el-select
              style="margin-left: 15px"
              v-model="queryParams.stationId"
              :clearable="true"
              filterable
              placeholder="请选择油站"
            >
              <el-option
                v-for="item in stationList"
                :key="item.stationId"
                :label="item.stationName"
                :value="item.stationId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="支付方式" prop="payType" clearable size="small">
            <el-select
              v-model="queryParams.payType"
              :clearable="true"
              filterable
              placeholder="请选择支付方式"
              style="margin-left: 15px"
            >
              <el-option label="现金支付" value="xj"></el-option>
              <el-option label="双屏机" value="spj"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="订单号" prop="orderCode" clearable size="small">
            <el-input
              style="margin-left: 15px"
              v-model="queryParams.orderCode"
              placeholder="请输入订单号"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="支付时间" prop="createdDate" size="small">
            <el-date-picker
              style="width: 20px; margin-left: 15px"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="dateRangeCreatedDate"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="changeDateRangeDate"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="global_el_rowStyle" :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
          size="mini"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          plain
          >重置</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-download"
          @click="handleExport"
          size="mini"
          plain
          >导出</el-button
        ></el-col
      >
    </el-row>

    <el-table
      style="width: 100%"
      :data="orderList"
      v-loading="loading"
      header-align="left"
      border
    >
      <el-table-column type="index" label="序号" width="60"></el-table-column>
      <el-table-column
        label="油企"
        align="left"
        prop="enterpriseName"
        v-if="[null].includes($store.state.user.userLevel)"
      >
      </el-table-column>
      <el-table-column
        label="油站"
        align="left"
        prop="stationName"
        v-if="!['2'].includes($store.state.user.userLevel)"
      >
      </el-table-column>
      <el-table-column label="订单号" align="left" prop="orderCode" width="180">
      </el-table-column>
      <el-table-column label="订单金额" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.payableAmount || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实付金额" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.actuallyAmount || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="left" prop="payType">
        <template slot-scope="scope">
          <span>{{ scope.row.payType == "spj" ? "双屏机" : "" }}</span>
          <span>{{ scope.row.payType == "xj" ? "现金支付" : "" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="left" prop="payTime" width="180">
      </el-table-column>
      <el-table-column label="订单状态" align="left" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.status == "0" ? "未支付" : "" }}</span>
          <span>{{ scope.row.status == "1" ? "支付成功" : "" }}</span>
          <span>{{ scope.row.status == "2" ? "支付失败" : "" }}</span>
          <span>{{ scope.row.status == "3" ? "已退款" : "" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="left"
        class-name="small-padding fixed-width"
        width="80px"
      >
        <!--fixed="right"-->
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            disable
            @click="detailsTable(scope.row)"
            >明细</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-drawer
      title="订单明细"
      :visible.sync="table"
      direction="rtl"
      size="50%"
    >
      <div style="margin: 20px; height: 73vh; overflow: auto">
        <el-button
          size="small"
          class="button-export"
          @click="handleExportDetail"
          >导出</el-button
        >

        <el-table :data="gridData" border>
          <el-table-column
            type="index"
            label="序号"
            width="60"
          ></el-table-column>
          <el-table-column
            property="categoryName"
            label="商品分类"
          ></el-table-column>
          <el-table-column
            property="commodityName"
            label="商品名称"
          ></el-table-column>
          <el-table-column
            property="barCode"
            label="商品条码"
          ></el-table-column>
          <el-table-column
            property="salePrice"
            label="商品单价"
          ></el-table-column>
          <el-table-column
            property="commodityNum"
            label="商品数量"
          ></el-table-column>
          <el-table-column property="totalPrice" label="合计"></el-table-column>
        </el-table>

        <el-button
          type="primary"
          size="small"
          class="dialog-button"
          @click="table = false"
          >关闭</el-button
        >
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { timeComparison } from "@/utils/timeConversion";
import {
  nonOilOrderList,
  enterpriseList,
  listEnterpriseStation,
  nonOilOrderExport,
  orderDetails,
  orderDetailsExport,
} from "@/api/nonOil/putlntoStorage";
export default {
  name: "NonOilOrder",
  data() {
    return {
      orderCode: null,
      table: false,
      enterpriseList: [],
      stationList: [],
      //时间间隔值
      dateRangeCreatedDate: [],
      // 总条数
      total: 0,
      // 统计数据
      workSum: {},
      //表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseId: null,
        stationId: null,
        // 开始时间
        beginTime: null,
        // 截止时间
        endTime: null,

        payType: null,
        orderCode: null,
      },
      isOnlyOneStation: false, // 当前油企是否只有一个正常使用的油站
      loading: false,
      gridData: [],
    };
  },
  created() {
    // 油企
    if (this.presentEnterprise && this.presentEnterprise.enterpriseId) {
      this.queryParams.enterpriseId = this.presentEnterprise.enterpriseId;
      this.getStationList(this.queryParams.enterpriseId);
    }
    // 油站账号登录
    if (this.presentSation) {
      this.queryParams.stationId = this.presentSation;
      this.handleQuery();
    }
    this.getList();
    this.getEnterpriseList();
  },

  methods: {
    // 选择了时间
    changeDateRangeDate() {
      // 开始时间
      this.queryParams.beginTime = this.dateRangeCreatedDate[0];
      // 截止时间
      this.queryParams.endTime = this.dateRangeCreatedDate[1];
    },

    // 请选择油企
    handleSelectChange(value) {
      this.stationList = [];
      this.queryParams.stationId = null;
      this.getStationList(value);
    },

    // 查询油企
    getEnterpriseList() {
      enterpriseList().then((res) => {
        this.enterpriseList = res.rows;
      });
    },

    // 查询油站
    getStationList(val) {
      listEnterpriseStation({ enterpriseId: val, status: "1" }).then((res) => {
        this.stationList = res;
        // 判断单站，多站
        if (res.length <= 1) {
          this.isOnlyOneStation = true;
        }
        if (res.length === 1) {
          this.queryParams.stationId = res[0].stationId;
          // this.handleQuery();
        }
      });
    },

    // 点击明细获取表格列表
    detailsTableList(row) {
      orderDetails({
        enterpriseId: this.queryParams.enterpriseId,
        stationId: this.queryParams.stationId,
        orderCode: row.orderCode,
      })
        .then((res) => {
          if (res && res.code === 200) {
            this.gridData = res.data;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(() => {});
    },
    // 点击明细
    detailsTable(row) {
      this.orderCode = row.orderCode;
      this.detailsTableList(row);
      this.table = true;
    },
    // 搜索列表
    getList() {
      this.loading = true;

      let queryParams = this.queryParams;

      if (!queryParams.payType) {
        queryParams.payType = null;
      }

      const time = timeComparison(
        30,
        "earching",
        queryParams.beginTime,
        queryParams.endTime
      );
      if (!time) return (this.loading = false);

      nonOilOrderList(queryParams)
        .then((response) => {
          this.loading = false;
          this.orderList = response.rows;
          this.total = response.total;
        })
        .catch(() => {
          this.loading = false;
          this.orderList = [];
          this.total = 0;
        });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreatedDate = [];
      // 开始时间
      this.queryParams.beginTime = null;
      // 截止时间
      this.queryParams.endTime = null;
      this.resetForm("queryForm");
      this.getList();
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;

      this.$confirm("是否确认导出所有数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return nonOilOrderExport(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },

    /** 详情导出按钮操作 */
    handleExportDetail() {
      const orderCode = this.orderCode;
      this.$confirm("是否确认导出所有数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return orderDetailsExport({ orderCode });
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.dialog-button {
  position: absolute;
  right: 20px;
  bottom: 20px;
}
.button-export {
  position: absolute;
  top: 55px;
  right: 20px;
}
.app-container {
  margin-top: -15px;
}

::v-deep .card-table {
  padding: 10px;
  margin-top: 0 !important;
  background-color: #f8f8f8;

  .el-button {
    margin: 0 5px 0 0;
  }
}

// 表格样式
::v-deep .el-form-item {
  margin-bottom: 0;

  .el-form-item__label {
    padding: 0;
    margin: 0;
    height: 29px;
    font-weight: 400;
    font-size: 12px;
  }

  .el-button--cyan {
    margin-center: 20px;
  }
}

::v-deep .el-table {
  border: 1px #f8f8f8 solid;
  border-bottom: 1px #dfe6ec solid;
}

::v-deep .is-center {
  border: 1px transparent solid;
  border-bottom: 1px #f8f8f8 solid;
}
</style>
